#define _CRT_SECURE_NO_WARNINGS
#define OK 1
#define ERROR -1
#include<stdio.h>
#include<malloc.h>

typedef struct StackNode
{
	int data;
	struct StackNode* next;
}StackNode, * LinkStack;


void Init_Stack(LinkStack &S)
{
	S = NULL;
	printf("初始化链栈成功!\n");
}

void Stack_Empty(LinkStack &S)
{
	if (S == NULL)
		printf("该链栈为空!\n");
	else
		printf("该链栈不为空!\n");
}

void Push_Stack(LinkStack &S, int e)
{
	LinkStack p = (LinkStack)malloc(sizeof(StackNode));
	p->data = e;
	p->next = S;
	S = p;
}

int Pop_Stack(LinkStack &S)
{
	if (S == NULL)
		return ERROR;
	int e = 0;
	e = S->data;
	LinkStack p = S;
	S = S->next;
	free(p);
	return e;
}

void GetTop_Stack(LinkStack &S)
{
	if (S != NULL)
		printf("栈顶元素为:%d\n", S->data);
	else
		printf("该链栈为空!\n");
}


void Traverse_Stack(LinkStack& S)
{
	if (S == NULL)
		printf("该链栈为空!\n");
	else
	{
		LinkStack p = S;
		printf("栈顶 ");
		while (p)
		{
			printf("%d ", p->data);
			p = p->next;
		}
		printf(" 栈底\n");
	}
}


int main()
{
	LinkStack S;
	printf("0.退出操作.\n");
	printf("1.初始化链栈.\n");
	printf("2.判断链栈是否为空.\n");
	printf("3.入栈.\n");
	printf("4.出栈.\n");
	printf("5.获取栈顶元素.\n");
	printf("6.遍历栈.\n");
	while (1)
	{
		int n = 0;
		int e = 0;
		int x = 0;
		printf("请选择对应操作:");
		scanf("%d", &n);
		switch (n)
		{
		case 0:
			x = 1;
			break;
		case 1:
			Init_Stack(S);
			break;
		case 2:
			Stack_Empty(S);
			break;
		case 3:
			printf("请输入入栈元素:");
			scanf("%d", &e);
			Push_Stack(S, e);
			break;
		case 4:
			e = Pop_Stack(S);
			if (e == -1)
				printf("该链栈为空!\n");
			else
				printf("出栈元素为:%d\n", e);
			break;
		case 5:
			GetTop_Stack(S);
			break;
		case 6:
			Traverse_Stack(S);
			break;
		}
		if (x == 1)
			break;
	}
	return 0;
}